package alpaca.view.panels;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import net.miginfocom.swing.MigLayout;
import alpaca.core.Message;
import alpaca.core.MessageList;

public class ShowLogFrame extends JFrame {

	private static final String EMPTY_LOG_MESSAGE = "There are no chat logs for the selected user(s) with the selected timeframe";
	private static final String CHAT_LOG_TITLE = "Chat Log";
	private JPanel contentPane;
	private final Action action = new CloseAction();

	/**
	 * Create the frame.
	 * @param ml MessageList with the messages to be shown.
	 */
	public ShowLogFrame(MessageList ml) {
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 606, 416);
		setTitle(CHAT_LOG_TITLE);
		setIconImage(new ImageIcon("img//alpaca.png").getImage());
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[grow]", "[grow][]"));
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), CHAT_LOG_TITLE, TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		contentPane.add(panel, "cell 0 0 1 2,grow");
		panel.setLayout(new MigLayout("", "[grow]", "[grow][]"));
		
		JScrollPane scrollPane = new JScrollPane();
		panel.add(scrollPane, "cell 0 0,grow");
		
		JTextArea textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		textArea.setBackground(SystemColor.text);
		textArea.setFont(new Font("Arial", Font.PLAIN, 12));
		textArea.setEditable(false);
		
		JButton btnNewButton = new JButton("New button");
		panel.add(btnNewButton, "cell 0 1,alignx right");
		btnNewButton.setAction(action);
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

		if (!ml.isEmpty()){
			for (Message msg : ml){
				textArea.append(msg.getFromUser() + " to " + msg.getToUser() 
						+ " at " + dateFormat.format(msg.getDate()) + ":" + System.getProperty ("line.separator") 
						+ msg.getMessage() + System.getProperty ("line.separator") + System.getProperty ("line.separator"));
			}
		} else {
			textArea.append(EMPTY_LOG_MESSAGE);
		}
		
		setVisible(true);
	}

	private class CloseAction extends AbstractAction {
		public CloseAction() {
			putValue(NAME, "Close");
			putValue(SHORT_DESCRIPTION, "Closes this window");
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			dispose();
		}
	}
}
